package com.dyzx.hbb.modules.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyzx.hbb.common.base.PageQuery;
import com.dyzx.hbb.common.base.PageResult;
import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseController;
import com.dyzx.hbb.modules.sys.entity.SysDict;
import com.dyzx.hbb.modules.sys.entity.SysDictValue;
import com.dyzx.hbb.modules.sys.query.DictQuery;
import com.dyzx.hbb.modules.sys.query.DictValueQuery;
import com.dyzx.hbb.modules.sys.service.SysDictService;
import com.dyzx.hbb.modules.sys.service.SysDictValueService;
import com.dyzx.hbb.modules.sys.vo.DictUVO;
import com.dyzx.hbb.modules.sys.vo.DictVO;
import com.dyzx.hbb.modules.sys.vo.DictValueUVO;
import com.dyzx.hbb.modules.sys.vo.DictValueVO;
import com.dyzx.hbb.security.context.SecurityContextHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/sys/dict")
@RestController
@Tag(name = "07.字典管理", description = "字典相关接口")
public class DictController extends BaseController<SysDictService, SysDict, DictQuery, DictVO, DictUVO> {

    @Autowired
    private SysDictService service;

    @Autowired
    private SysDictValueService valueService;

    @Override
    protected SysDictService getBaseService() {
        return service;
    }


    @Override
    protected MyLambdaQueryWrapper<SysDict> queryWrapper(DictQuery query) {
        System.out.println("queryWrapper");
        System.out.println(query);
        MyLambdaQueryWrapper<SysDict> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(SysDict::getId)
                .select(SysDict::getName)
                .select(SysDict::getKeyStr)
                .select(SysDict::getRemark)
                .select(SysDict::getStatus)
                .select(SysDict::getCreateDate).select(SysDict::getUpdateDate);
        if (query != null) {


            queryWrapper.orderByDesc(SysDict::getId);
            if (StringUtils.isNotEmpty(query.getName())) {
                queryWrapper.likeRight(SysDict::getName, query.getName());
            }

            if (StringUtils.isNotEmpty(query.getRemark())) {
                queryWrapper.likeRight(SysDict::getRemark, query.getRemark());
            }
            if (StringUtils.isNotEmpty(query.getStart_date())) {
                queryWrapper.apply(StringUtils.isNotBlank(query.getStart_date()), "DATE(create_date)>=DATE_FORMAT('" + query.getStart_date() + "','%Y-%m-%d')");
            }

            if (StringUtils.isNotEmpty(query.getEnd_date())) {
                queryWrapper.apply(StringUtils.isNotBlank(query.getEnd_date()), "DATE(create_date)<=DATE_FORMAT('" + query.getEnd_date() + "','%Y-%m-%d')");
            }
            if (StringUtils.isNotEmpty(query.getKey_str())) {
                queryWrapper.likeRight(SysDict::getKeyStr, query.getKey_str());
            }
            if (query.getStatus() > 0) {
                queryWrapper.eq(SysDict::getStatus, query.getStatus());
            }
            return queryWrapper;
        } else {
            return queryWrapper;
        }

    }

    @Override
    protected MyLambdaQueryWrapper<SysDict> queryInfoWrapper() {
        MyLambdaQueryWrapper<SysDict> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysDict::getId, SysDict::getName, SysDict::getStatus, SysDict::getCreateDate, SysDict::getUpdateDate, SysDict::getRemark, SysDict::getKeyStr);
        return wrapper;
    }

    @Operation(description = "删除")
    @DeleteMapping("value/{id}")
    public Result<Boolean> deleteValue(
            @PathVariable Long id
    ) {

        if (id > 0) {
            boolean update = valueService.removeById(id);
            if (update) {
                //  redisUtil.delAll("findByKeys");
                return Result.ok();
            } else {
                return Result.error("删除失败");
            }
        } else {

            return Result.error("参数错误");
        }

    }


    @Operation(description = "修改")
    //   @CacheEvict(cacheNames = {"valueListByDictId"})
    @PutMapping("value")
    public Result<Boolean> valueEdit(@RequestBody DictValueUVO entity) {

        if (StringUtils.isNotEmpty(entity.getName()) && entity.getValue() != null) {
            SysDictValue data = new SysDictValue();
            BeanUtil.copyProperties(data, entity);
            boolean flag = false;

            flag = valueService.updateById(data);


            return flag ? Result.ok() : Result.error("添加修改");
        } else {
            return Result.error("参数错误");
        }

    }

    @Operation(description = "添加")
    //   @CacheEvict(cacheNames = {"valueListByDictId"})
    @PostMapping("value")
    public Result<Boolean> valueAdd(@RequestBody DictValueVO entity) {

        if (StringUtils.isNotEmpty(entity.getName()) && entity.getValue() != null) {
            SysDictValue data = new SysDictValue();
            BeanUtil.copyProperties(entity,data);
            boolean flag = false;
            System.out.println(entity);
            System.out.println(data);

            flag = valueService.save(data);


            return flag ? Result.ok() : Result.error("添加失败");
        } else {
            return Result.error("参数错误");
        }

    }


    @Operation(description = "数据词典查询（编号）")
    @GetMapping("value/list")
    //   @Cacheable(cacheNames = "valueListByDictId", key = "#dictId")
    public Result<SysDictValue> valueListByDictId(DictValueQuery query,PageQuery pageQuery

    ) {
        System.out.println("valueListByDictId");
        System.out.println(query);
        System.out.println(pageQuery);

        return valueService.findByPage(pageQuery,query);
    }

    @Operation(description = "获取数据词典（关键字）")
    @GetMapping("value/key")
    public Result<List<Map<String, Object>>> valueListByKey(@RequestParam(value = "dictkey") String dictkey) {

        List<Map<String, Object>> list = valueService.findByKey(dictkey);
        return !list.isEmpty() ? Result.ok(list) : Result.error("没有数据");
    }

    @Operation(description = "获取数据词典（关键字）")
    @GetMapping("value/all")
    public Result<Map<String, List<Map<String, Object>>>> valueListByAll() {

        Map<String, List<Map<String, Object>>> list = valueService.findByKeyAll();
        return !list.isEmpty() ? Result.ok(list) : Result.error("没有数据");
    }

}
